#!/usr/bin/env python
#-*- encoding: utf-8 -*-

import wx
import codecs

class MainFrame(wx.Frame):
    """
    A Frame that translation po
    """

    def __init__(self, *args, **kw):
        # ensure the parent's __init__ is called
        super(MainFrame, self).__init__(*args, **kw)

        # create a panel in the frame
        panel = wx.Panel(self)

        # and put some text with a larger bold font on it
        st = wx.StaticText(panel, label="输入英文", pos=(10, 0))
        self.etxt = wx.TextCtrl(panel, -1,"",(10,20), size=(350, 100))
        self.etxt.SetInsertionPoint(0)

        wx.StaticText(panel, -1, "输入中文", (10, 120))
        self.ctxt = wx.TextCtrl(panel, -1, "",(10,140), size=(350, 200))

        self.button = wx.Button(panel, -1, "增加", pos=(100, 350))
        self.Bind(wx.EVT_BUTTON, self.onClick, self.button)
        self.button.Enable(False)

        self.start = wx.Button(panel, -1, "开始", pos=(10, 350))
        self.Bind(wx.EVT_BUTTON, self.startw, self.start)

        self.end = wx.Button(panel, -1, "关闭", pos=(200, 350))
        self.Bind(wx.EVT_BUTTON, self.endw, self.end)
        self.end.Enable(False)
        self.Show()
        # create a menu bar
        self.makeMenuBar()

        # and a status bar
        self.CreateStatusBar()
        self.SetStatusText("作者：雷中听风")

    def onClick(self, event):
        self.entxt = self.etxt.GetValue()
        self.zntxt = self.ctxt.GetValue()

        self.ff.writelines("msgid   " + '"' + self.entxt + '"\n')
        self.ff.writelines("msgstr  " + '"' + self.zntxt + '"\n')
        self.ff.writelines("\n")

        self.etxt.SetValue("")
        self.ctxt.SetValue("")

    def startw(self, event):
        self.ff = codecs.open("pyfa.po", "a", 'utf-8')
        self.button.Enable(True)
        self.start.Enable(False)
        self.end.Enable(True)

    def endw(self, event):
        self.ff.close()
        wx.Exit()
    def makeMenuBar(self):
        """
        A menu bar is composed of menus, which are composed of menu items.
        This method builds a set of menus and binds handlers to be called
        when the menu item is selected.
        """

        # Make a file menu with Hello and Exit items
        fileMenu = wx.Menu()

        # The "\t..." syntax defines an accelerator key that also triggers
        # the same event
        helloItem = fileMenu.Append(-1, "&说明\tCtrl-H",
                "Help string shown in status bar for this menu item")
        fileMenu.AppendSeparator()
        # When using a stock ID we don't need to specify the menu item's
        # label
        exitItem = fileMenu.Append(wx.ID_EXIT,"退出")

        # Now a help menu for the about item
        helpMenu = wx.Menu()
        aboutItem = helpMenu.Append(wx.ID_ABOUT,"关于")

        # Make the menu bar and add the two menus to it. The '&' defines
        # that the next letter is the "mnemonic" for the menu item. On the
        # platforms that support it those letters are underlined and can be
        # triggered from the keyboard.
        menuBar = wx.MenuBar()
        menuBar.Append(fileMenu, "&文件")
        menuBar.Append(helpMenu, "&帮助")

        # Give the menu bar to the frame
        self.SetMenuBar(menuBar)

        # Finally, associate a handler function with the EVT_MENU event for
        # each of the menu items. That means that when that menu item is
        # activated then the associated handler function will be called.
        self.Bind(wx.EVT_MENU, self.OnHello, helloItem)
        self.Bind(wx.EVT_MENU, self.OnExit,  exitItem)
        self.Bind(wx.EVT_MENU, self.OnAbout, aboutItem)


    def OnExit(self, event):
        """Close the frame, terminating the application."""
        self.Close(True)


    def OnHello(self, event):
        """Say hello to the user."""
        wx.MessageBox("Hello again from wxPython")


    def OnAbout(self, event):
        """Display an About Dialog"""
        wx.MessageBox("这是一个EVE物品数据翻译器。",
                      "关于EVE物品数据翻译器",
                      wx.OK|wx.ICON_INFORMATION)


if __name__ == '__main__':
    # When this module is run (not imported) then create the app, the
    # frame, show it, and start the event loop.
    app = wx.App()
    frm = MainFrame(None, title='EVE物品数据翻译器')
    frm.Show()
    app.MainLoop()